<?

$current = $_GET['current'];
if (!isset($current)) $current = 0;

if(empty($_SESSION['admin_filter_vysledky'])) {
	$filter = new FilterVysledky();
	$filter->datum_do = StrFTime("%Y-%m-%d", Time());
} else {
	$filter = $_SESSION['admin_filter_vysledky'];
}

if(empty($_SESSION['admin_filter2_vysledky'])) {
	$filter2 = new FilterVysledky();
	$filter2->trida = 2;
} else {
	$filter2 = $_SESSION['admin_filter2_vysledky'];
}


if($action == ACTION_SAVE_MATCH_FILTER) {
	$filter->kolo = $_POST['a_filtr_kolo'];
	$filter->datum_od = $_POST['a_filtr_od'];
	$filter->datum_do = $_POST['a_filtr_do'];
	$_SESSION['admin_filter_vysledky'] = $filter;
}

if($action == ACTION_CLEAR_MATCH_FILTER) {
	$filter = new FilterVysledky();
	$_SESSION['admin_filter_vysledky'] = $filter;
}

if($action == ACTION_SAVE_MATCH_FILTER_2) {
	$filter2->trida = $_POST['a_filtr2_trida'];
	$_SESSION['admin_filter2_vysledky'] = $filter2;
}

if($action == ACTION_CLEAR_MATCH_FILTER_2) {
	$filter2->trida = 2;
	$_SESSION['admin_filter2_vysledky'] = $filter2;
}

$a_vysledek_id = $_GET['id_utkani'];

if($a_vysledek_id != ""){
	$query = getResult($a_vysledek_id);
	$fetch = MySQL_Fetch_Array($query);
	$a_vysledek_id_domaci = $fetch["ID_DOMACI"];
	$a_vysledek_id_hoste = $fetch["ID_HOSTE"];
	$a_vysledek_b_domaci = $fetch["B_DOMACI"];
	$a_vysledek_b_hoste = $fetch["B_HOSTE"];
	$a_vysledek_kolo = $fetch["KOLO"];
	$a_vysledek_soutezni = $fetch["SOUTEZNI"];
	$a_vysledek_sezona = $fetch["SEZONA"];
	$a_vysledek_pozn = $fetch["POZNAMKA"];
	$a_vysledek_datum = substr($fetch["DATUM"], 0, 16);
	if($fetch["trida_domaci"] != $fetch["trida_hoste"]) {
		$filter2->trida = "";
	}
} else {
	$a_vysledek_datum = StrFTime("%Y-%m-%d %H:%M", Time());
	$a_vysledek_soutezni = 1;
	$a_vysledek_sezona = SEASON;
	$a_vysledek_kolo = "";
	$a_vysledek_pozn = "";
}

$query = getResultsCount($filter);
$fetch = MySQL_Fetch_Array($query);

$paginator = new Paginator(10, 3, $current, $fetch["POCET"], PAGE_ADMIN_RESULTS, "");

?>

<script language="JavaScript" type="text/javascript"
	src="./js/calendar.js">
</script>

<span class="msg" id="msg">Pro snažší nalezení požadovaných utkání je
zde filtr,<br>který umožní vyhledání podle konkrétního kola nebo data
odehrání.</span>

<div class="center">

<div class="centered"><img onmouseover="MyTip('msg', 'help')"
	onmouseout="UnTip()" class='icon-btn' src='./img/icons/help.png'></div>

<div class="filter-col">
<form name="formUtkaniFilter"
	action="<? echo redirect_str(PAGE_ADMIN_RESULTS).action(ACTION_SAVE_MATCH_FILTER); ?>" method="post">

<table class="centered small-table">
	<tr>
		<th colspan=3>Filtr utkání</th>
	</tr>

	<tr>
		<td class="lefted">kolo<br />
		<input class='text px120' type='text' maxlength="2"
			name='a_filtr_kolo' value="<? echo $filter->kolo; ?>" /></td>
	</tr>
	<tr>	
		<td class="lefted">datum od<br />
		<input class='text px120' type='text' maxlength="10" name='a_filtr_od'
			value="<? echo $filter->datum_od; ?>" /><img class="icon-btn"
			onClick="ShowCalendar('formUtkaniFilter', 'a_filtr_od', true)"
			src="./img/icons/calendar.gif"></td>
	</tr>
	<tr>
		<td class="lefted">datum do<br />
		<input class='text px120' type='text' maxlength="10" name='a_filtr_do'
			value="<? echo $filter->datum_do; ?>" /><img class="icon-btn"
			onClick="ShowCalendar('formUtkaniFilter', 'a_filtr_do', true)"
			src="./img/icons/calendar.gif"></td>
	</tr>

	<tr>
		<td colspan=3>
			<div class="centered">
				<input class="button"
					onmouseover="this.className='button_hover'"
					onmouseout="this.className='button'" type="submit" value="FILTROVAT">
				<a class="button preloader" href="<? echo redirect_str(PAGE_ADMIN_RESULTS).action(ACTION_CLEAR_MATCH_FILTER);?>">ZRUŠIT FILTR</a>
			</div>
		</td>
	</tr>
</table>

</form>
</div>

<div class="results-col">
<table class="centered small-table">
	<tr>
		<th>datum</th>
		<th>kolo</th>
		<th>domácí</th>
		<th>výsledek</th>
		<th>hosté</th>
		<th>akce</th>
	</tr>
	<?php

	$query = getResultsFromTo($paginator->getFirstPageIndex(), $paginator->getPageSubjects(), $filter);
	$idx = 0;
	while($fetch = MySQL_Fetch_Array($query)) {
		$id_utkani = $fetch["id_utkani"];
		$datum = DateFormat::format($fetch["datum"], DF_DDMMYYYYHHMM);
		$kolo = $fetch["kolo"];
		$domaci = $fetch["domaci"];
		$hoste = $fetch["hoste"];
		$soutezni = $fetch["soutezni"];
		$vysledek = $fetch["b_domaci"]." : ".$fetch["b_hoste"];


		if($soutezni == 1) {
			if($idx % 2 == 0) { echo "<tr class='second'>"; }
			else echo "<tr>";
		}
		else echo "<tr class='disabled'>";
		echo "<td>$datum</td><td>$kolo</td><td class='righted'>$domaci</td>";
		echo "<td>$vysledek</td><td class='lefted'>$hoste</td>";
		echo "<td><a class='preloader' href=\"".redirect_str(PAGE_ADMIN_RESULTS).add_par("id_utkani=".$id_utkani)."\"><img class='icon-btn' src='./img/icons/edit.png'></a>
                <a class='preloader' href=\"".redirect_str(PAGE_ADMIN_RESULTS).action(ACTION_DELETE_MATCH).add_par("id_del_utkani=".$id_utkani)."\"><img class='icon-btn' src='./img/icons/delete.png'></a></td>";
		echo "</tr>";
		$idx ++;
	}
	?>

	<tr>
		<td colspan="6"><? $paginator->show(); ?></td>
	</tr>
</table>
</div>

<div class="correct"><hr></div>


<div class="filter-col">
	<form action="<? echo redirect_str(PAGE_ADMIN_RESULTS).action(ACTION_SAVE_MATCH_FILTER_2); ?>"
		method="post">
	
	<table class="centered small-table">
		<tr>
			<th>Filtr mužstev dle soutěží</th>
		</tr>
	
		<tr>
			<td class="lefted">třída<br/>
			<select name='a_filtr2_trida' class="text" size=1 width="10">
				<option value="">vše</option>
				<?
				for($i=2; $i<=4; $i++){
					if($i == $filter2->trida) echo "<option value='$i' selected>$i</option>";
					else echo "<option value='$i'>$i</option>";
				}
				?>
			</select></td>
		</tr>
		<tr>
			<td>
				<div class="centered">
					<input class="button preloader"
						onmouseover="this.className='button_hover'"
						onmouseout="this.className='button'" type="submit" value="FILTROVAT">
					<a class="button preloader" href="<? echo redirect_str(PAGE_ADMIN_RESULTS).action(ACTION_CLEAR_MATCH_FILTER_2);?>">ZRUŠIT FILTR</a>
				</div>
			</td>
		</tr>
	</table>
	
	</form>
</div>

<div class="results-col">
	<form name="formAdminVysledek" action="<? echo redirect_str(PAGE_ADMIN_RESULTS).action(ACTION_SAVE_MATCH); ?>"method="post">
	
	<input type="hidden" name="a_vysledek_id" value="<? echo $a_vysledek_id;?>">
	
	<table class="centered small-table">
		<tr>
			<th>datum</th>
			<th>domácí</th>
			<th colspan="2">výsl.</th>
			<th>hosté</th>
		</tr>
		<tr>
			<td><input class='text px120' type='text' maxlength="16"
				name='a_vysledek_datum' value="<? echo $a_vysledek_datum; ?>" /> <img
				class="icon-btn"
				onClick="ShowCalendar('formAdminVysledek', 'a_vysledek_datum', false)"
				src="./img/icons/calendar.gif"></td>
			<td><select class="select text" name='a_vysledek_id_domaci' size=1>
			<option value="-1">..vyberte mužstvo</option>
			<?
			$query = getTeams($filter2->trida);
			while($fetch = MySQL_Fetch_Array($query)){
				$id_muzstvo = $fetch["ID_MUZSTVO"];
				$nazev = $fetch["NAZEV"];
				if($id_muzstvo == $a_vysledek_id_domaci)
					echo "<option value='$id_muzstvo' selected>".$nazev."</option>";
				else
					echo "<option value='$id_muzstvo'>".$nazev."</option>";
			}
			?>
			</select></td>
			<td><input class='text px20' type='text' maxlength="2"
				name='a_vysledek_b_domaci' value="<? echo $a_vysledek_b_domaci; ?>" />
			</td>
			<td><input class='text px20' type='text' maxlength="2"
				name='a_vysledek_b_hoste' value="<? echo $a_vysledek_b_hoste; ?>" />
			</td>
			<td><select class="select text" name='a_vysledek_id_hoste' size=1>
			<option value="-1">..vyberte mužstvo</option>
			<?
			$query = getTeams($filter2->trida);
			while($fetch = MySQL_Fetch_Array($query)){
				$id_muzstvo = $fetch["ID_MUZSTVO"];
				$nazev = $fetch["NAZEV"];
				if($id_muzstvo == $a_vysledek_id_hoste)
				echo "<option value='$id_muzstvo' selected>".$nazev."</option>";
				else
				echo "<option value='$id_muzstvo'>".$nazev."</option>";
			}
			?>
			</select></td>
		
		
		<tr>
			<td colspan="3" class="righted">mistrovské utkání</td>
			<td colspan="3" class="lefted"><input type="checkbox"
				onchange="changeVisibility('a_vysledek_kolo');"
				name="a_vysledek_soutezni" value="1"
				<? echo ($a_vysledek_soutezni == 1?"checked":""); ?>></td>
		</tr>
		<tr id='a_vysledek_kolo' class="<? echo ($a_vysledek_soutezni == 1?"":"hidden"); ?>">
			<td colspan="3" class="righted">kolo</td>
			<td colspan="3" class="lefted"><input class="text px20"
				type='text' maxlength="2" name='a_vysledek_kolo'
				value="<? echo $a_vysledek_kolo; ?>" /></td>
		</tr>
		<tr>
			<td colspan="3" class="righted">sezona</td>
			<td colspan="3" class="lefted"><select class='text' name='a_vysledek_sezona' size=1>
			<?
			for($rok = SEASON; $rok >= 2005; $rok--){
				if($rok == $a_vysledek_sezona) echo "<option value='$rok' selected>$rok/".($rok+1)."</option>";
				else echo "<option value='$rok'>$rok/".($rok+1)."</option>";
			}
			?>
			</select></td>
		</tr>
		<tr>
			<td colspan="3" class="righted">poznámka</td>
			<td colspan="3" class="lefted"><input type="text" class='text px170'
				name='a_vysledek_pozn' value="<? echo $a_vysledek_pozn; ?>" /></td>
		</tr>
		
		<tr>
			<td colspan="6">
				<input class="button"
					onmouseover="this.className='button_hover'"
					onmouseout="this.className='button'" type="submit" value="ULOŽIT">
				<?
				if($a_vysledek_id != ""){
					echo "<a href=\"".redirect_str(PAGE_ADMIN_RESULTS)."\" class='button'>ZRUŠIT</a>";
				}
				?>
			</td>
		</tr>
	</table>
	
	</form>
</div>

<div class="correct"></div>

</div>
